package com.wgts.wgts.mvp.presenter.imp;

import android.view.View;

import com.wgts.wgts.mvp.model.IGuessingAndAwardFragmentModel;
import com.wgts.wgts.mvp.model.IJoinActionActivityModel;
import com.wgts.wgts.mvp.presenter.IGuessingAndAwardFragmentPresenter;
import com.wgts.wgts.mvp.view.IGuessingAndAwardFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AwardListReq;
import com.wgts.wgts.netmodle.resp.AwardPrizeEntity;
import com.wgts.wgts.netmodle.resp.GuessingAndAwardOrderEntity;

import java.util.List;

/*
 *
 *
 *   data:2018/10/1311:53
 *
 */public class GuessingAndAwardFragmentPresenter implements IGuessingAndAwardFragmentPresenter {
     private IGuessingAndAwardFragmentView view;
     private IGuessingAndAwardFragmentModel model;

     private IJoinActionActivityModel joinActionActivityModel;

     private int pageIndex = 1;
     private int pageSize = 20;
     private Integer drawAllType;

     private int pageSumSize = 0;

    public GuessingAndAwardFragmentPresenter(IGuessingAndAwardFragmentView view, IGuessingAndAwardFragmentModel model, IJoinActionActivityModel joinActionActivityModel) {
        this.view = view;
        this.model = model;
        this.joinActionActivityModel = joinActionActivityModel;
    }

    @Override
    public void initOrderList(Integer drawAllType) {
        pageIndex = 1;
        this.drawAllType = drawAllType;

        pageSumSize = 0;
        model.initOrderList(drawAllType, pageIndex, pageSize, new HttpResultSubscriber<List<GuessingAndAwardOrderEntity>>() {
            @Override
            public void onSuccess(List<GuessingAndAwardOrderEntity> guessingAndAwardOrderEntities) {
                for (GuessingAndAwardOrderEntity  entity: guessingAndAwardOrderEntities){
                    pageSumSize += entity.list.size();
                }
                if (pageSumSize < pageSize){
                    view.initOrderList(guessingAndAwardOrderEntities, true);
                }else {
                    view.initOrderList(guessingAndAwardOrderEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void loadMore(Integer drawAllType) {

        pageSumSize = 0;

        model.initOrderList(drawAllType, pageIndex, pageSize, new HttpResultSubscriber<List<GuessingAndAwardOrderEntity>>() {
            @Override
            public void onSuccess(List<GuessingAndAwardOrderEntity> orderEntities) {

                for (GuessingAndAwardOrderEntity  entity: orderEntities){
                    pageSumSize += entity.list.size();
                }

                if (pageSumSize < pageSize){
                    view.loadMoreOrderList(orderEntities, true);
                }else {
                    view.loadMoreOrderList(orderEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void awardPrizeList(Integer drawAllType) {
        pageIndex = 1;
        this.drawAllType = drawAllType;
        AwardListReq req = new AwardListReq();
        req.drawAllType = drawAllType;
        req.pageIndex = pageIndex;
        req.pageSize = pageSize;
        model.awardPrizeList(req, new HttpResultSubscriber<List<AwardPrizeEntity>>() {
            @Override
            public void onSuccess(List<AwardPrizeEntity> awardPrizeEntities) {
                if(awardPrizeEntities.size() < pageSize){
                    view.initAwardList(awardPrizeEntities, true);
                }else{
                    view.initAwardList(awardPrizeEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void moreAwardPrizeList() {
        AwardListReq req = new AwardListReq();
        req.drawAllType = drawAllType;
        req.pageIndex = pageIndex;
        req.pageSize = pageSize;
        model.awardPrizeList(req, new HttpResultSubscriber<List<AwardPrizeEntity>>() {
            @Override
            public void onSuccess(List<AwardPrizeEntity> awardPrizeEntities) {
                if(awardPrizeEntities.size() < pageSize){
                    view.loadMoreAwardList(awardPrizeEntities, true);
                }else{
                    view.loadMoreAwardList(awardPrizeEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void initJoinActivityList(Integer drawAllType) {
        this.pageIndex = 1;
        this.drawAllType = drawAllType;

        pageSumSize = 0;

        joinActionActivityModel.joinActivityList(drawAllType, this.pageIndex, this.pageSize, new HttpResultSubscriber<List<GuessingAndAwardOrderEntity>>() {
            @Override
            public void onSuccess(List<GuessingAndAwardOrderEntity> entities) {

                for (GuessingAndAwardOrderEntity  entity: entities){
                    pageSumSize += entity.list.size();
                }

                if(pageSumSize < pageSize){
                    view.initActionList(entities, true);
                }else{
                    view.initActionList(entities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void loadMoreJoinActivityList() {

        pageSumSize = 0;

        joinActionActivityModel.joinActivityList(drawAllType, this.pageIndex, this.pageSize, new HttpResultSubscriber<List<GuessingAndAwardOrderEntity>>() {
            @Override
            public void onSuccess(List<GuessingAndAwardOrderEntity> entities) {

                for (GuessingAndAwardOrderEntity  entity: entities){
                    pageSumSize += entity.list.size();
                }

                if(pageSumSize < pageSize){
                    view.loadMoreActionList(entities, true);
                }else{
                    view.loadMoreActionList(entities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


}
